package com.example.campustalent.activity

import android.Manifest
import android.Manifest.permission.READ_EXTERNAL_STORAGE
import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
import android.content.pm.PackageManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import com.bumptech.glide.Glide
import com.example.campustalent.R
import com.example.campustalent.bean.ContentBean
import com.example.campustalent.databinding.ActivityRegisterBinding
import com.example.campustalent.databinding.ActivityReleaseBinding
import com.example.campustalent.utils.DbUtils
import com.example.campustalent.utils.GlideEngine
import com.example.campustalent.utils.SharedPreferencesUtils
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.listener.OnResultCallbackListener
import com.luck.picture.lib.style.PictureSelectorUIStyle
import com.luck.picture.lib.tools.ToastUtils

class ReleaseActivity : AppCompatActivity() {
    private lateinit var binding: ActivityReleaseBinding
    val CAMERA = 13
    val WRITE_EXTERNAL_STORAGE = 11
    val READ_EXTERNAL_STORAGE = 12
    private lateinit var preferencesUtils: SharedPreferencesUtils
    private var mUrl: String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (DbUtils.liteOrm == null) {
            DbUtils.createDb(this, "CampusTalent")
        }
        preferencesUtils = SharedPreferencesUtils(this)
        binding = ActivityReleaseBinding.inflate(layoutInflater)
        val rootView: View = binding.root
        setContentView(rootView)
        binding.ivImage.setOnClickListener {
            finish()
        }

        binding.tvUpload.setOnClickListener {
            if (binding.etContent.text.toString().isBlank()){
                ToastUtils.s(this, "文本不能为空")
                return@setOnClickListener
            }
            val content = ContentBean()
            content.content = binding.etContent.text.toString()
            content.imageUrl = mUrl
            content.name = preferencesUtils.getParam("name", "").toString()
            DbUtils.insert(content)
            val list = DbUtils.getQueryByWhere(ContentBean::class.java, "name", arrayOf(preferencesUtils.getParam("name", "").toString()))
            if (list.isNullOrEmpty()){
                ToastUtils.s(this, "kongde ")
            }
            ToastUtils.s(this, "上传成功")
            finish()
        }
        binding.ivImage.setOnClickListener {
            finish()
        }
        binding.ivAdd.setOnClickListener {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//                    requestPermissions(
//                        arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
//                        WRITE_EXTERNAL_STORAGE
//                    )
//                }
//                if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//                    requestPermissions(
//                        arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
//                        READ_EXTERNAL_STORAGE
//                    )
//                }
                if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(arrayOf(Manifest.permission.CAMERA), CAMERA)
                }
            }
            PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .theme(R.style.picture_default_style)
                .isCompress(true)
                .isCamera(false)
                .isEnableCrop(true)
                .circleDimmedLayer(true)
                .showCropGrid(false)
                .rotateEnabled(false)
                .withAspectRatio(1, 1)
                .selectionMode(PictureConfig.SINGLE)
                .setPictureUIStyle(PictureSelectorUIStyle.ofDefaultStyle())
                .imageEngine(GlideEngine.createGlideEngine()) // Please refer to the Demo GlideEngine.java
                .forResult(object : OnResultCallbackListener<LocalMedia> {
                    override fun onResult(result: List<LocalMedia>?) {
                        if (result != null && result.isNotEmpty()) {
                            if (result[0] != null) {
                                binding.ivImage.visibility = View.VISIBLE
                                binding.ivAdd.visibility = View.GONE
                                if (result[0].isCut) {
                                    mUrl = result[0].cutPath
                                    Glide.with(this@ReleaseActivity).load(result[0].cutPath).into(binding.ivImage)
                                } else {
                                    mUrl = result[0].compressPath
                                    Glide.with(this@ReleaseActivity).load(result[0].compressPath).into(binding.ivImage)
                                }
                            }
                        }
                    }

                    override fun onCancel() {}
                })
        }
    }
}